import sys
import time
import signal
import os

sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "library")))

from service.service import Service

exit = False


def aiit():
    # https://patorjk.com/software/taag/
    print(
        r"""
  _________                      __    _________                
 /   _____/ _____ _____ ________/  |_  \_   ___ \_____ _______  
 \_____  \ /     \\__  \\_  __ \   __\ /    \  \/\__  \\_  __ \ 
 /        \  Y Y  \/ __ \|  | \/|  |   \     \____/ __ \|  | \/ 
/_______  /__|_|  (____  /__|   |__|    \______  (____  /__|    
        \/      \/     \/                      \/     \/                                            
    """
    )


def handler(signum, frame):
    print("")
    global exit
    exit = True


def main():
    signal.signal(signal.SIGINT, handler)
    signal.signal(signal.SIGTERM, handler)

    aiit()

    service = Service()
    ret = service.init()
    if not ret:
        print("service init failed")
        sys.exit()

    ret = service.start()
    if not ret:
        print("service start failed")
        service.deinit()
        sys.exit()

    while not exit:
        time.sleep(0.2)

    service.stop()
    service.deinit()


if __name__ == "__main__":
    main()
